#include "TcpServer.hpp"
#include "Protocol.hpp"

static void Usage(const std::string &proc)
{
    std::cout << "\nUsage: " << proc << "port\n\n"
              << std::endl;
}
//./serverCal 8080
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port = std::stoi(argv[1]);
    ServerCal cal;
    // 端口号，回调函数，这个回调函数为传入一个string类型,返回值为string类型的函数
    // cal是ServerCal类型，Calculator是ServerCal类型的成员函数
    // 它将 ServerCal::Calculator 成员函数与 cal 对象绑定，
    // 并且预留了一个占位符 _1，表示传入的第一个参数传给Calculator函数的第一个参数
    TcpServer *tsvp = new TcpServer(8080, std::bind(&ServerCal::Calculator, &cal, std::placeholders::_1));
    tsvp->InitServer();
    tsvp->Start();

    std::cout << "______________________________" << std::endl;
    Response resp(1000, 0); 
    std::string content1;
    resp.Serialize(&content1);
    std::cout << content1 << std::endl;
    std::string package1 = Encode(content1); // 添加报头形成报文
    std::cout << package1;

    content1 = "";
    bool r = Decode(package1, &content1);
    std::cout << content1 << std::endl;
    Response temp1;
    temp1.Deserialize(content1);
    std::cout << temp1.code_ << std::endl;
    std::cout << temp1.result_ << std::endl;

    Request req(123, 456, '+');
    std::string s;
    req.Serialize(&s);
    s = Encode(s); // 添加报头
    std::cout << s;

    std::string content;
    // bool r = Decode(s, &content); // 从报文中去除报头，提取正文
    std::cout << "报文有效载荷为：" << content << std::endl;

    Request temp;
    temp.Deserialize(content);
    // std::cout << re << std::endl;
    std::cout << temp.x << std::endl;
    std::cout << temp.op << std::endl;
    std::cout << temp.y << std::endl;
    return 0;
}